<?php
namespace app\admin\model;
use think\Model;
use think\model\concern\SoftDelete;

class Goods extends Model
{
    use SoftDelete;
    protected $deleteTime='delete_time';
    //搜索器
    public function searchKeywordsAttr($q,$v)     {$q->where('g.name','like',"%{$v}%");}
    public function searchIsDownAttr($q,$v)       {$q->where('g.is_down','=',$v);}
    public function searchIsShowAttr($q,$v)       {$q->where('g.is_show','=',$v);}
    public function searchIsHotAttr($q,$v)        {$q->where('g.is_hot','=',$v);}
    public function searchCidAttr($q,$v)          {$q->where('g.cid','=',$v);}
    //获取器
    public function getAddtimeAttr($v)            {$v=date('Y-m-d H:i:s',$v);return $v;}
    //商城展示搜索
    public static function goodList($where)
    {
        $list=Goods::withTrashed()
            ->alias('g')
            ->withSearch(array_keys($where),$where)
            ->join("category c","g.cid=c.id")
            ->field("g.name,g.id,g.price,g.photo_x,g.is_show,g.is_hot,g.is_down,g.addtime,c.name cname")
            ->order("g.id")
            ->where("g.delete_time is null")
            ->paginate([
                'list_rows'=>8,
                'query'=>$where
            ]);
        return  $list;
    }
    //商城删除处理
    public static function goodDel($delid)
    {
        $goods=Goods::withTrashed()->find($delid);
        $goodss=$goods->delete();
        return $goodss;
    }
    //商品删除后的恢复处理
    public static function goodRollback($ids)
    {

        $user = Goods::onlyTrashed()->find($ids);
        $user->restore();
    }
}
